﻿namespace LeadMaster.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using LeadMaster.Models;
    using LeadMaster.DAL;
    using LeadMaster.ViewModels;
    using System.Web.Security;
    using System.Threading;

    public class EnquiryController : Controller
    {
        private DunsmoreContext db = new DunsmoreContext();
        bool InCreationMode = false;

        public ActionResult Index(int id)
        {
            ViewModelEnquiry viewModel = new ViewModelEnquiry();
            viewModel.Enquiries = db.Enquiry
                                .Where(e => e.LeadID == id)
                                .Include(e => e.Contact)
                                .Include(e => e.Company)
                                .OrderByDescending(m => m.EnquiryDate)
                                .ToList();

            return PartialView(viewModel);
        }

        public ActionResult ShowEnquiries(int id)
        {
            ViewModelEnquiry viewModel = new ViewModelEnquiry();

            viewModel.Enquiries = db.Enquiry
                                .Where(e => e.LeadID == id)
                                .Include(e => e.Contact)
                                .Include(e => e.Company)
                                .OrderByDescending(m => m.EnquiryDate)
                                .ToList();

            Leads lead = db.Leads.Find(id);
            viewModel.Company = db.Companies.Find(lead.CompanyID);

            return PartialView("Index",viewModel);
        }

        public ActionResult Details(int id = 0)
        {
            Enquiries enquiry = db.Enquiry.Find(id);
            if (enquiry == null)
            {
                return HttpNotFound();
            }
            return View(enquiry);
        }

        public ActionResult Create(int id)
        {
            ViewModelEnquiry viewModel = new ViewModelEnquiry();

            viewModel.Lead = db.Leads.Find(id);
            viewModel.Enquiry = new Enquiries();
            viewModel.Enquiry.CompanyID = viewModel.Lead.CompanyID;
            viewModel.Enquiry.UserID = (Guid)Membership.GetUser().ProviderUserKey;
            viewModel.Enquiry.ContactID = viewModel.Lead.ContactID;
            viewModel.Enquiry.LeadID = id;
            viewModel.Enquiry.ContactID = viewModel.Lead.ContactID;
            viewModel.Enquiry.EnquiryTypeID = db.Enquiry
                                    .Where(e => e.LeadID == id)
                                    .OrderByDescending(e => e.EnquiryDate)
                                    .Select(e => e.EnquiryTypeID)
                                    .FirstOrDefault();
                                    
            viewModel.EnquiryHoldingDate = DateTime.Now.ToString("dd/MM/yyyy");
            viewModel.EnquiryHoldingTime = DateTime.Now.ToString("HH:mm");
            viewModel.FullName = string.Format("{0} {1}", viewModel.Lead.Contact.Forename, viewModel.Lead.Contact.Surname);

            viewModel.ContactsList = db.Contacts
                                    .ToList()
                                    .Where(m => m.CompanyID == viewModel.Lead.CompanyID)
                                    .OrderBy(m => m.Surname)
                                    .ThenBy(m => m.Forename)
                                    .Select(c => new SelectListItem
                                    {
                                        Value = c.ContactID.ToString(),
                                        Text = string.Format("{0} {1}", c.Forename, c.Surname)
                                    });

            ViewBag.EnquiryTypeID = new SelectList(db.EnquiryTypes, "EnquiryTypeID", "EnquiryType", viewModel.Enquiry.EnquiryTypeID);

            return PartialView(viewModel);
        }

        //
        // POST: /Enquiry/Create

        [HttpPost]
        public void Create(ViewModelEnquiry viewModel)
        {
            if (ModelState.IsValid)
            {
                viewModel.Enquiry.ReceptiveID = 1; // not in use yet
                string intDateTime = viewModel.EnquiryHoldingDate + " " + viewModel.EnquiryHoldingTime;
                viewModel.Enquiry.EnquiryDate = Convert.ToDateTime(intDateTime);
                db.Enquiry.Add(viewModel.Enquiry);
                db.SaveChanges();
            }
        }

        //
        // GET: /Enquiry/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Enquiries enquiry = db.Enquiry.Find(id);
            if (enquiry == null)
            {
                return HttpNotFound();
            }
            ViewBag.ContactID = new SelectList(db.Contacts, "ContactID", "Forename", enquiry.ContactID);
            ViewBag.CompanyID = new SelectList(db.Companies, "CompanyID", "Company", enquiry.CompanyID);
            return View(enquiry);
        }

        //
        // POST: /Enquiry/Edit/5

        [HttpPost]
        public ActionResult Edit(Enquiries enquiry)
        {
            if (ModelState.IsValid)
            {
                db.Entry(enquiry).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ContactID = new SelectList(db.Contacts, "ContactID", "Forename", enquiry.ContactID);
            ViewBag.CompanyID = new SelectList(db.Companies, "CompanyID", "Company", enquiry.CompanyID);
            return View(enquiry);
        }

        //
        // GET: /Enquiry/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Enquiries enquiry = db.Enquiry.Find(id);
            if (enquiry == null)
            {
                return HttpNotFound();
            }
            return View(enquiry);
        }

        //
        // POST: /Enquiry/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Enquiries enquiry = db.Enquiry.Find(id);
            db.Enquiry.Remove(enquiry);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}